package com.hs.mod_archive.helper

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.hs.framework.utils.getStringFromResource
import com.hs.mod_archive.R
import com.hs.mod_archive.databinding.ItemCabinetBinding
import com.hs.mod_archive.view.MyListView
import com.hs.room.model.archive.DevCabinet


class DevCabinetHelper(
    footPageListView: MyListView, private val itemClickListener: OnItemClickListener?,
    footSearchCallback: FootSearchCallback?
) :
    ListViewHelper(footPageListView, footSearchCallback) {
    private val mContext: Context = footPageListView.context
    var data: DevCabinet? = null
    private val viewHolders = arrayOfNulls<ItemCabinetBinding>(
        perPageCount
    )
    private val mParentView: LinearLayout = footPageListView.mViewList

    private var datas: List<DevCabinet>? = null

    fun setListDataAndRefresh(entityList: List<DevCabinet>?) {
        datas = entityList
        if (null == viewHolders[0]) {
            for (i in 0 until perPageCount) {
                val binding =
                    ItemCabinetBinding.inflate(
                        LayoutInflater.from(mContext),
                        mParentView,
                        true
                    )
                viewHolders[i] = binding
                if (isEvenOrOdd(i)) {
                    binding.root.setBackgroundColor(mColorBgOddNumberLine)
                }
                binding.tvOpt.setOnClickListener { itemClickListener!!.onItemClickDelete(i) }
            }
        }
        if (datas!!.isNotEmpty()) {
            mParentView.visibility = View.VISIBLE
            val newListSize = datas!!.size
            for (i in 0 until perPageCount) {
                if (i >= newListSize) {
                    val recordBinding = viewHolders[i]
                    recordBinding!!.root.visibility = View.INVISIBLE
                } else {
                    val holder = viewHolders[i]
                    holder!!.root.visibility = View.VISIBLE
                    data = datas!![i]
                    holder.tvLockNo.text = "${data!!.localId}"
                    holder.tvName.text = data!!.name
                    holder.tvBoard.text = "${data!!.boardId}"
                    holder.tvLockNo.text = "${data!!.lockNo}"
                    holder.tvLampBoard.text = "${data!!.lampBoardId}"
                    holder.tvLampNo.text = "${data!!.lampNo}"
                    holder.tvStatus.setText(
                        if (0 == data!!.status) getStringFromResource(R.string.enable) else getStringFromResource(
                            R.string.disable
                        )
                    )
                }
            }
        } else {
            mParentView.visibility = View.INVISIBLE
            for (binding in viewHolders) {
                binding!!.root.visibility = View.INVISIBLE
            }
        }
    }
}
